package com.isnmp.nms.framework.discover.service;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.isnmp.common.snmp.SNMPFactory;
import com.isnmp.common.snmp.SNMPTarget;
import com.isnmp.common.snmp.impl.SNMPUtils;
import com.isnmp.common.snmp.mib.Dot1dBasePortEntry;
import com.isnmp.common.snmp.mib.Dot1dTpFdbEntry;
import com.isnmp.common.snmp.mib.MibIfEntry;
import com.isnmp.common.snmp.mib.MibMacIP;
import com.isnmp.common.snmp.mib.MibSystem;
import com.isnmp.nms.framework.discover.DeviceInfo;
import com.isnmp.nms.framework.discover.PortInfo;

public class SearchBasicDeviceInfoService {

    /**
     * 
     * @param ip
     * @param paramList
     * @param isUsePing  是否使用icmp true:使用  false:不使用
     * @return
     */
    public DeviceInfo getDeviceBasicInfo(String ip, List<SNMPTarget> snmpTargetList, boolean isUsePing) {
        DeviceInfo localDeviceInfo = new DeviceInfo();
        try {
            MibSystem mibSystem = null;
            SNMPTarget localSNMPTarget = new SNMPTarget();
            Object localObject2 = snmpTargetList.iterator();
            Object localObject1;
            while (((Iterator) localObject2).hasNext()) {
                localObject1 = (SNMPTarget) ((Iterator) localObject2).next();
                localSNMPTarget.nodeIP = ip;
                localSNMPTarget.port = ((SNMPTarget) localObject1).port;
                localSNMPTarget.readCommunity = ((SNMPTarget) localObject1).readCommunity;
                localSNMPTarget.snmpVersion = ((SNMPTarget) localObject1).snmpVersion;
                try {
                    mibSystem = (MibSystem) SNMPFactory.getSNMPAPI().getMibObject(new MibSystem(), localSNMPTarget);
                    //System.out.println(Messages.getString("SearchBasicDeviceInfoService.0") + ip);
                    localDeviceInfo.setDeviceDesc(mibSystem.getSysDescr());
                    localDeviceInfo.setDeviceIP(ip);
                    localDeviceInfo.setDeviceName(mibSystem.getSysName());
                    localDeviceInfo.setDeviceProtocol("SNMP");
                    localDeviceInfo.setDeviceSysOID(mibSystem.getSysObjectID());
                    localDeviceInfo.setSnmpTarget(localSNMPTarget);
                    getMainMAC(localDeviceInfo);
                    getInterfaceInfo(localDeviceInfo);
                    getMacIPInfo(localDeviceInfo);
                    getDocFPTableInfo(localDeviceInfo);
                    return localDeviceInfo;
                } catch (Exception localException2) {
                }
            }

            if ((isUsePing) && (SNMPUtils.canPing(ip))) {
                localObject1 = InetAddress.getByName(ip);
                localObject2 = ((InetAddress) localObject1).getCanonicalHostName();
                localDeviceInfo.setDeviceIP(ip);
                localDeviceInfo.setDeviceName((String) localObject2);
                localDeviceInfo.setDeviceProtocol("ICMP");
                return localDeviceInfo;
            }
        } catch (Exception localException1) {
            //localException1.printStackTrace();
        }
        return null;
    }

    private void getMainMAC(DeviceInfo paramDeviceInfo) {
        try {
            String str = SNMPFactory.getSNMPAPI().getOIDValue("1.3.6.1.2.1.17.1.1.0", paramDeviceInfo.getSnmpTarget());
            str = SNMPUtils.formatDispayMacAddress(str);
            paramDeviceInfo.setDeviceMAC(str);
            if (!str.equals(""))
                paramDeviceInfo.getDeviceMacList().add(str);
        } catch (Exception localException) {
            System.out.println(Messages.getString("SearchBasicDeviceInfoService.3") + paramDeviceInfo.getDeviceIP() + Messages.getString("SearchBasicDeviceInfoService.4") + localException.getMessage());
        }
    }

    private void getInterfaceInfo(DeviceInfo paramDeviceInfo) {
        try {
            List localList = SNMPFactory.getSNMPAPI().getAllTableData(MibIfEntry.class, paramDeviceInfo.getSnmpTarget());
            Iterator localIterator = localList.iterator();
            while (localIterator.hasNext()) {
                MibIfEntry localMibIfEntry = (MibIfEntry) localIterator.next();
                String str = localMibIfEntry.getIfPhysAddress();
                str = SNMPUtils.formatDispayMacAddress(str);
                localMibIfEntry.setIfPhysAddress(str);
                paramDeviceInfo.getIfTableList().add(localMibIfEntry);
                if ((!str.equals("")) && (!paramDeviceInfo.getDeviceMacList().contains(str)))
                    paramDeviceInfo.getDeviceMacList().add(str);
            }
        } catch (Exception localException) {
            System.out.println(Messages.getString("SearchBasicDeviceInfoService.6") + paramDeviceInfo.getDeviceIP() + Messages.getString("SearchBasicDeviceInfoService.7") + localException.getMessage());
        }
    }

    private void getMacIPInfo(DeviceInfo paramDeviceInfo) {
        try {
            List localList = SNMPFactory.getSNMPAPI().getAllTableData(MibMacIP.class, paramDeviceInfo.getSnmpTarget());
            Iterator localIterator = localList.iterator();
            while (localIterator.hasNext()) {
                MibMacIP localMibMacIP = (MibMacIP) localIterator.next();
                if (localMibMacIP.getIpNetToMediaType() != 2) {
                    String str = SNMPUtils.formatDispayMacAddress(localMibMacIP.getIpNetToMediaPhysAddress());
                    localMibMacIP.setIpNetToMediaPhysAddress(str);
                    if (!str.equals(""))
                        paramDeviceInfo.getMibMacIPList().add(localMibMacIP);
                }
            }
        } catch (Exception localException) {
            System.out.println(Messages.getString("SearchBasicDeviceInfoService.9") + paramDeviceInfo.getDeviceIP() + Messages.getString("SearchBasicDeviceInfoService.10") + localException.getMessage());
        }
    }

    private void getDocFPTableInfo(DeviceInfo paramDeviceInfo) {
        try {
            List localList1 = SNMPFactory.getSNMPAPI().getAllTableData(Dot1dTpFdbEntry.class, paramDeviceInfo.getSnmpTarget());
            List localList2 = SNMPFactory.getSNMPAPI().getAllTableData(Dot1dBasePortEntry.class, paramDeviceInfo.getSnmpTarget());
            paramDeviceInfo.setBasePortList(localList2);
            Object localObject1 = localList1.iterator();
            while (((Iterator) localObject1).hasNext()) {
                Dot1dTpFdbEntry localDot1dTpFdbEntry = (Dot1dTpFdbEntry) ((Iterator) localObject1).next();
                Object localObject2 = localDot1dTpFdbEntry.getDot1dTpFdbAddress();
                localObject2 = SNMPUtils.formatDispayMacAddress((String) localObject2);
                localDot1dTpFdbEntry.setDot1dTpFdbAddress((String) localObject2);
            }
            boolean bool = false;
            if (localList1.size() <= 0) {
                bool = true;
                Object localObject2 = paramDeviceInfo.getMibMacIPList().iterator();
                while (((Iterator) localObject2).hasNext()) {
                    localObject1 = (MibMacIP) ((Iterator) localObject2).next();
                    Object localObject3 = new Dot1dTpFdbEntry();
                    ((Dot1dTpFdbEntry) localObject3).setDot1dTpFdbAddress(((MibMacIP) localObject1).getIpNetToMediaPhysAddress());
                    ((Dot1dTpFdbEntry) localObject3).setDot1dTpFdbPort(((MibMacIP) localObject1).getIpNetToMediaIfIndex());
                    ((Dot1dTpFdbEntry) localObject3).setDot1dTpFdbStatus(3);
                    if (((MibMacIP) localObject1).getIpNetToMediaType() != 2)
                        localList1.add(localObject3);
                }
            }
            localObject1 = new HashMap();
            Object localObject3 = localList1.iterator();
            Object localObject5;
            while (((Iterator) localObject3).hasNext()) {
                Object localObject2 = (Dot1dTpFdbEntry) ((Iterator) localObject3).next();
                if ((((Dot1dTpFdbEntry) localObject2).getDot1dTpFdbStatus() != 2) && (((Dot1dTpFdbEntry) localObject2).getDot1dTpFdbStatus() != 4)) {
                    Object localObject4 = ((Dot1dTpFdbEntry) localObject2).getDot1dTpFdbAddress();
                    paramDeviceInfo.getdTpFdbTableList().add((Dot1dTpFdbEntry) localObject2);
                    localObject5 = (List) ((HashMap) localObject1).get(Integer.valueOf(((Dot1dTpFdbEntry) localObject2).getDot1dTpFdbPort()));
                    if (localObject5 == null) {
                        localObject5 = new ArrayList();
                        ((HashMap) localObject1).put(Integer.valueOf(((Dot1dTpFdbEntry) localObject2).getDot1dTpFdbPort()), localObject5);
                    }
                    if ((!((String) localObject4).equals("")) && (!((List) localObject5).contains(localObject4)))
                        ((List) localObject5).add(localObject4);
                }
            }
            Object localObject2 = ((HashMap) localObject1).entrySet();
            Object localObject4 = ((Set) localObject2).iterator();
            while (((Iterator) localObject4).hasNext()) {
                localObject3 = (Map.Entry) ((Iterator) localObject4).next();
                localObject5 = new PortInfo();
                ((PortInfo) localObject5).device = paramDeviceInfo;
                ((PortInfo) localObject5).port = ((Integer) ((Map.Entry) localObject3).getKey()).intValue();
                ((PortInfo) localObject5).isUseIfIndexPort = bool;
                ((PortInfo) localObject5).portMacList = ((List) ((Map.Entry) localObject3).getValue());
                paramDeviceInfo.getPortInfoList().add((PortInfo) localObject5);
            }
        } catch (Exception localException) {
            System.out.println(Messages.getString("SearchBasicDeviceInfoService.12") + paramDeviceInfo.getDeviceIP() + Messages.getString("SearchBasicDeviceInfoService.13") + localException.getMessage());
        }
    }
}